<?php

namespace app\index\controller;

use app\common\CommonResponse;
use app\common\model\Activite;
use app\index\validate\AssessmentValidate;
use Cassandra\Uuid;
use think\App;
use think\Controller;
use think\Request;

class Assessment extends Base
{
    protected $accessmentModel;
    protected $fileModel;
    protected $userModel;
    protected $activiteModel;
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->accessmentModel=new \app\common\model\Assessment();
        $this->fileModel=new \app\common\model\File();
        $this->userModel=new \app\common\model\User();
        $this->activiteModel=new Activite();
    }

    public function list(){
        $assessments = $this->accessmentModel->where("user_id", $this->user['id'])->paginate(10);
        foreach ($assessments as $index=>$assessment){
            $activites = $this->activiteModel->where("assessment_id", $assessment['id'])->select();
            $process=(($activites->where("state","!=",0)->count())/($activites->count()))*100;
            $fileList = $this->fileModel->whereIn("id",implode(',', json_decode($assessment["files"])))->select();
            $scores=$this->activiteModel->field('sum(score) as score')->find();
            $assessments[$index]["activites"]=$activites;
            $assessments[$index]['fileList']=$fileList;
            $assessments[$index]['process']=$process;
            $assessments[$index]['scores']=$scores['score'];
        }
        $this->assign("assessments",$assessments);
        return $this->fetch();
    }

    public function addAssessment(Request $request){
        if($request->isPost()){
            $assessment=$request->post();
            $check = new AssessmentValidate();
            if(!$check->check($assessment)){
                return $this->error($check->getError());
            }
            $assessment['id']=\uuid();
            $assessment['user_id']=$this->user['id'];
            $activiteUserIds=$assessment['activite_user_id'];
            $activite=[
                "assessment_id"=>$assessment['id'],
                "state"=>0
            ];
            foreach ($activiteUserIds as $activiteUserId ){
                $activite['teacher_id']=$activiteUserId;
                $this->activiteModel->insert($activite);
            }
            $assessment['activite_user_id']=json_encode($assessment['activite_user_id']);
            $assessment['files']=json_encode($assessment['files']);
            $assessment['user_id']=$this->user['id'];
            $assessment['state']=0;
            $assessment['create_time']=time();
            $saveRes = $this->accessmentModel->save($assessment);
            if(!$saveRes){
                return $this->error("申请评审失败");
            }
            return $this->success("申请评审成功");
        }
        $files = $this->fileModel->where("user_id", $this->user['id'])->select();
        $powerTeachers=$this->userModel->getPowerTeacher();
        $this->assign("files",$files);
        $this->assign("powerTeachers",$powerTeachers);
        return $this->fetch();
    }

    public function editScore(Request $request){
        $id=$request->post("id");
        $score=$request->post("score");
        if(!$id || !$score){
            return $this->error("id和成绩不能为空");
        }
        $assessment = $this->accessmentModel::get($id);
        $activite = $this->activiteModel->where(["assessment_id" => $id, "teacher_id" => $this->user['id']])->find();
        if(!$activite){
            return$this->error("找不到该打分项");
        }
        if($activite['state']!=0){
            return$this->error("已经打过分数");
        }
        $activite->score=$score;
        $activite->state=1;
        $saveRes = $activite->save();
        if(!$saveRes){
            return $this->error("打分失败");
        }
        $activiteCount = $this->activiteModel->where(["assessment_id" => $id, "state" => 0])->count();
        if($activiteCount==0){
            //改变评审表状态
            $assessment->state=1;
            $assessment->finaltime=time();
            $assessment->save();
            //发送通知短信
            $user = $this->userModel->where("id", $assessment['user_id'])->find();
            $send_notice_sms = send_notice_sms($user['phone']);
            if(!$send_notice_sms){
                return $this->error("打分成功，发送通知短信失败");
            }
        }
        return $this->success("打分成功");
    }

}
